﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccessLibrary;
using BusinessLayer.DTO;

namespace BusinessLayer
{
    public class ClsAttendance
    {
        public int EmployeeID { get; set; }
        public int AttendanceID { get; set; }
        public DateTime? DateAttended { get; set; }
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }
        public void GetEmployeeAttendance(int employeeID)
        {
            using (OleDataClient data = new OleDataClient())
            {
                DataTable dt = data.GetDataTable("Select * from Attendance where Format([DateAttended],'yyyy-mm-dd')='" + Utility.GetDateYYYYMMDD(DateTime.Now) + "' and EmployeeID=" + employeeID.ToString(), "Attendance");
                if (dt.Rows.Count > 0)
                {
                    AttendanceID = Convert.ToInt32(dt.Rows[0]["AttendanceID"]);
                    DateAttended = Utility.GetValidDateElseNull(dt.Rows[0]["DateAttended"]);
                    StartTime = Utility.GetValidDateElseNull(dt.Rows[0]["StartTime"]);
                    EndTime = Utility.GetValidDateElseNull(dt.Rows[0]["EndTime"]);
                }
            }
        }
        public DataTable GetEmployeeAttendance(int employeeID, DateTime fromDate, DateTime toDate)
        {
            using (OleDataClient data = new OleDataClient())
            {
                DataTable dt = data.GetDataTable("Select * from Attendance where Format([DateAttended],'yyyy-mm-dd') between '" + Utility.GetDateYYYYMMDD(fromDate) + "' and '" + Utility.GetDateYYYYMMDD(toDate) + "' and EmployeeID=" + employeeID.ToString(), "Attendance");
                return dt;
            }
        }

        public void UpdateEmployeeAttendanceStartTime(int employeeID, DateTime DateAttended, DateTime StartTime)
        {
            using (OleDataClient data = new OleDataClient())
            {
                try
                {
                    data.ExecuteNonQuery("Update Attendance set StartTime = '" + StartTime + "' where  Format([DateAttended],'yyyy-mm-dd')='" + Utility.GetDateYYYYMMDD(DateAttended) + "' and EmployeeID=" + employeeID.ToString());
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
        public void UpdateEmployeeAttendanceEndTime(int employeeID, DateTime DateAttended, DateTime EndTime)
        {
            using (OleDataClient data = new OleDataClient())
            {
                try
                {
                    if (IsINDataPresent(employeeID, DateAttended))
                    {
                        data.ExecuteNonQuery("Update Attendance set EndTime = '" + EndTime + "' where  Format([DateAttended],'yyyy-mm-dd')='" + Utility.GetDateYYYYMMDD(DateAttended) + "' and EmployeeID=" + employeeID.ToString());
                    }
                    else
                    {
                        throw new Exception("IN time not present, Please set IN time before OUT.");
                    }

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        public bool IsINDataPresent(int employeeID, DateTime dateTime)
        {
            using (OleDataClient data = new OleDataClient())
            {
                string count = data.GetValue("Select count(*) from Attendance where Format([DateAttended],'yyyy-mm-dd')='" + Utility.GetDateYYYYMMDD(dateTime) + "' and StartTime IS NOT NULL  and EmployeeID=" + employeeID.ToString());
                if (Convert.ToInt32(count) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public void SaveEmployeeAttendance(string KeyForSave, int employeeID, DateTime dateTime)
        {
            using (OleDataClient data = new OleDataClient())
            {
                if (KeyForSave == "IN")
                {
                    string count = data.GetValue("Select count(*) from Attendance where Format([DateAttended],'yyyy-mm-dd')='" + Utility.GetDateYYYYMMDD(dateTime) + "' and EmployeeID=" + employeeID.ToString());
                    if (Convert.ToInt32(count) == 0)
                    {
                        data.ExecuteNonQuery("Insert into Attendance (EmployeeID,DateAttended,StartTime)"
                        + " values('" + employeeID + "','" + dateTime.ToShortDateString() + "','" + dateTime + "')");
                    }
                }
                else
                {
                    if (IsINDataPresent(employeeID, dateTime))
                    {
                        data.ExecuteNonQuery("Update Attendance set EndTime = '" + dateTime + "' where Format([DateAttended],'yyyy-mm-dd')='" + Utility.GetDateYYYYMMDD(dateTime) + "' and EmployeeID=" + employeeID.ToString());
                    }
                    else
                    {
                        throw new Exception("IN time not present, Please set IN time before OUT.");
                    }
                }
            }
        }
        public void UpdateEmployeeAttendance(List<AttendanceInfo> lstAttendanceInfo)
        {
            using (OleDataClient data = new OleDataClient())
            {
                try
                {
                    data.StartTran();
                    foreach (AttendanceInfo item in lstAttendanceInfo)
                    {
                        DateTime? StartTime = item.StartTime;
                        if (item.StartTime.HasValue)
                        {
                            StartTime = new DateTime(item.DateAttended.Value.Year, item.DateAttended.Value.Month, item.DateAttended.Value.Day, item.StartTime.Value.Hour, item.StartTime.Value.Minute, item.StartTime.Value.Second);
                            data.ExecuteNonQuery("Update Attendance set StartTime = '" + StartTime + "'  where  AttendanceID=" + item.AttendanceID.ToString());
                        }
                        else
                        {
                            throw new Exception("Please provide IN time for " + item.DateAttended.Value.ToShortDateString() + " and submit again.");
                        }
                        DateTime? EndTime = item.EndTime;
                        if (item.EndTime.HasValue)
                        {
                            if (!StartTime.HasValue)
                            {
                                throw new Exception("Please provide IN time for " + item.DateAttended.Value.ToShortDateString() + " and submit again.");
                            }
                            EndTime = new DateTime(item.DateAttended.Value.Year, item.DateAttended.Value.Month, item.DateAttended.Value.Day, item.EndTime.Value.Hour, item.EndTime.Value.Minute, item.EndTime.Value.Second);
                            data.ExecuteNonQuery("Update Attendance set  EndTime = '" + EndTime + "' where  AttendanceID=" + item.AttendanceID.ToString());
                        }
                        else
                        {
                            data.ExecuteNonQuery("Update Attendance set  EndTime = NULL  where  AttendanceID=" + item.AttendanceID.ToString());
                        }
                    }
                    data.CommitTran();
                }
                catch (Exception ex)
                {
                    data.RollBackTran();
                    throw ex;
                }

            }
        }
    }
}
